package com.skiving.cloud.develop.service.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.solon.service.impl.ServiceImpl;
import com.skiving.cloud.common.exception.BusinessException;
import com.skiving.cloud.common.utils.PageParameter;
import com.skiving.cloud.develop.entity.SysBusinessDesigner;
import com.skiving.cloud.develop.entity.SysFormDesigner;
import com.skiving.cloud.develop.mapper.SysBusinessDesignerMapper;
import com.skiving.cloud.develop.mapper.SysFormDesignerMapper;
import com.skiving.cloud.develop.service.SysBusinessDesignerService;
import org.noear.solon.annotation.Component;
import org.noear.solon.Utils;
import org.noear.solon.annotation.Inject;

/**
 * 应用设计记录 服务层实现。
 *
 * @author skivingcloud
 * @since 2025-03-25
 */
@Component
public class SysBusinessDesignerServiceImpl extends ServiceImpl<SysBusinessDesignerMapper, SysBusinessDesigner>  implements SysBusinessDesignerService {
    @Inject
    private SysFormDesignerMapper sysFormDesignerMapper;
    /**
     * 分页查询应用设计记录
     *
     * @param parameter 查询参数
     * @return 分页查询结果
     */
    @Override
    public Page<SysBusinessDesigner> queryByPage(PageParameter<SysBusinessDesigner> parameter){
        Page<SysBusinessDesigner> page = new Page<>(parameter.getPage(), parameter.getPageSize());
        QueryWrapper queryWrapper = QueryWrapper.create();
        if (parameter.getParam() != null) {
            //查询条件
            if (Utils.isNotBlank(parameter.getParam().getId())) {
                queryWrapper.and(SysBusinessDesigner::getId).like(parameter.getParam().getId());
            }
            if (Utils.isNotBlank(parameter.getParam().getDesignerCode())) {
                queryWrapper.and(SysBusinessDesigner::getDesignerCode).like(parameter.getParam().getDesignerCode());
            }
            if (Utils.isNotBlank(parameter.getParam().getDesignerName())) {
                queryWrapper.and(SysBusinessDesigner::getDesignerName).like(parameter.getParam().getDesignerName());
            }
            if (Utils.isNotBlank(parameter.getParam().getTypeCode())) {
                queryWrapper.and(SysBusinessDesigner::getTypeCode).like(parameter.getParam().getTypeCode());
            }
            if (Utils.isNotBlank(parameter.getParam().getHasTask())) {
                queryWrapper.and(SysBusinessDesigner::getHasTask).like(parameter.getParam().getHasTask());
            }
            if (Utils.isNotBlank(parameter.getParam().getTaskDefCode())) {
                queryWrapper.and(SysBusinessDesigner::getTaskDefCode).like(parameter.getParam().getTaskDefCode());
            }
            if (Utils.isNotBlank(parameter.getParam().getNewFormId())) {
                queryWrapper.and(SysBusinessDesigner::getNewFormId).like(parameter.getParam().getNewFormId());
            }
            if (Utils.isNotBlank(parameter.getParam().getModFormId())) {
                queryWrapper.and(SysBusinessDesigner::getModFormId).like(parameter.getParam().getModFormId());
            }
            if (Utils.isNotBlank(parameter.getParam().getDetailFormId())) {
                queryWrapper.and(SysBusinessDesigner::getDetailFormId).like(parameter.getParam().getDetailFormId());
            }
            if (Utils.isNotBlank(parameter.getParam().getMenuTxt())) {
                queryWrapper.and(SysBusinessDesigner::getMenuTxt).like(parameter.getParam().getMenuTxt());
            }
            if (Utils.isNotBlank(parameter.getParam().getCustomizeApi())) {
                queryWrapper.and(SysBusinessDesigner::getCustomizeApi).like(parameter.getParam().getCustomizeApi());
            }
            if (Utils.isNotBlank(parameter.getParam().getAddApiUrl())) {
                queryWrapper.and(SysBusinessDesigner::getAddApiUrl).like(parameter.getParam().getAddApiUrl());
            }
            if (Utils.isNotBlank(parameter.getParam().getUpdateApiUrl())) {
                queryWrapper.and(SysBusinessDesigner::getUpdateApiUrl).like(parameter.getParam().getUpdateApiUrl());
            }
            if (Utils.isNotBlank(parameter.getParam().getDetailApiUrl())) {
                queryWrapper.and(SysBusinessDesigner::getDetailApiUrl).like(parameter.getParam().getDetailApiUrl());
            }
        }
        return this.page(page, queryWrapper);
    }

    /**
     * 根据编号获取表单信息
     *
     * @param designerCode 表单设计编号
     * @param type 类型：1-新增表单、2-修改表单、3-详情表单
     * @return 表单设计信息
     */
    @Override
    public SysBusinessDesigner getSysBusinessDesignerByCodeHasJson(String designerCode, String type) throws BusinessException {
        QueryWrapper queryWrapper = QueryWrapper.create().where(SysBusinessDesigner::getDesignerCode).eq(designerCode);
        SysBusinessDesigner sysBusinessDesigner = this.getOne(queryWrapper);
        if("1".equals(type)){
            SysFormDesigner sysFormDesigner = sysFormDesignerMapper.selectOneById(sysBusinessDesigner.getNewFormId());
            sysBusinessDesigner.setNewFormJson(sysFormDesigner.getFormDesJson());
        }else if("2".equals(type)){
            SysFormDesigner sysFormDesigner = sysFormDesignerMapper.selectOneById(sysBusinessDesigner.getModFormId());
            sysBusinessDesigner.setModFormJson(sysFormDesigner.getFormDesJson());
        }else if("3".equals(type)){
            SysFormDesigner sysFormDesigner = sysFormDesignerMapper.selectOneById(sysBusinessDesigner.getDetailFormId());
            sysBusinessDesigner.setDetailFormJson(sysFormDesigner.getFormDesJson());
        }else{
            throw new BusinessException("请传入正确的表单类型");
        }
        return sysBusinessDesigner;
    }
}
